﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Collections;
using System.Data;

namespace DataLayer
{
    public class Factory
    {
        static List<IDbConnection> conList = new List<IDbConnection>();
        static Factory nesne = new Factory();
        string provider = System.Configuration.ConfigurationSettings.AppSettings["Provider"];
        public string Provider
        {
            get { return provider; }
            set { provider = value; }
        }

        string cnnString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
        public string CnnString
        {
            get { return cnnString; }
            set { cnnString = value; }
        }

        Database createdObject;
        private Hashtable paramCache = Hashtable.Synchronized(new Hashtable());

        public void JoinTable(DataSet ds, DataTable tablo, string tableName)
        {
            if (ds.Tables.Contains(tableName))
                ds.Tables.Remove(tableName);
            tablo.TableName = tableName;
            ds.Tables.Add(tablo);
        }

        #region Parametre
        public string GetParameterToken()
        {
            return createdObject.GetParameterToken();
        }
        #region Generic
        public IDbDataParameter GetParameter<T>(string paramName, DbType tip, T value)
        {
            return createdObject.CreateParam<T>(paramName, tip, value);
        }
        public IDbDataParameter GetParameter<T>(string paramName, DbType tip, T value, Int32 size)
        {
            return createdObject.CreateParam<T>(paramName, tip, value, size);
        }
        #endregion
        #region Collection
        public IDbDataParameter GetParameter(string paramName, DbType tip, object value, Int32 size, ParameterDirection direction)
        {
            IDbDataParameter prm = createdObject.CreateParam(paramName, tip, value, size);
            prm.Direction = direction;
            return prm;
        }
        public IDbDataParameter GetParameter(string paramName, DbType tip, object value, Int32 size)
        {
            return createdObject.CreateParam(paramName, tip, value, size);
        }

        public IDbDataParameter GetParameter(string paramName, DbType tip, object value, ParameterDirection direction)
        {
            IDbDataParameter prm = createdObject.CreateParam(paramName, tip, value);
            prm.Direction = direction;
            return prm;
        }
        public IDbDataParameter GetParameter(string paramName, DbType tip, object value)
        {
            return createdObject.CreateParam(paramName, tip, value);
        }

        public IDbDataParameter GetParameter(string paramName, Int32 size, DbType tip)
        {
            return createdObject.CreateParam(paramName, size, tip);
        }
        public IDbDataParameter GetParameter(string paramName, Int32 size, DbType tip, ParameterDirection direction)
        {
            IDbDataParameter prm = createdObject.CreateParam(paramName, size, tip);
            prm.Direction = direction;
            return prm;
        }

        public IDbDataParameter GetParameter(string paramName, DbType tip)
        {
            return createdObject.CreateParam(paramName, tip);
        }
        public IDbDataParameter GetParameter(string paramName, DbType tip, ParameterDirection direction)
        {
            IDbDataParameter prm = createdObject.CreateParam(paramName, tip);
            prm.Direction = direction;
            return prm;
        }
        #endregion
        #endregion

        #region Singleton Ctor
        private Factory()
        {
            if (createdObject == null)
            {
                Type database = Type.GetType(provider);
                ConstructorInfo constructor = database.GetConstructor(new Type[] { });
                createdObject = (Database)constructor.Invoke(null);
            }
        }
        public static Factory GetFactory
        {
            get
            {
                return nesne;
            }
        }
        #endregion

        #region Database Connection
        IDbConnection GetConnection()
        {
            bool bulundu = false;
            IDbConnection cn = null;
            foreach (IDbConnection item in conList)
            {
                if (item.State == ConnectionState.Closed)
                {
                    cn = item;
                    cn.ConnectionString = CnnString;
                    cn.Open();
                    bulundu = true;
                    break;
                }
            }
            if (bulundu == false)
            {
                cn = createdObject.CreateConnection(CnnString);
                cn.Open();
                conList.Add(cn);
            }
            return cn;
        }

        #endregion

        #region Caching
        public IDbDataParameter[] CloneParameters(IDbDataParameter[] originalParameters)
        {
            IDbDataParameter[] clonedParameters = new IDbDataParameter[originalParameters.Length];

            for (int i = 0, j = originalParameters.Length; i < j; i++)
            {
                clonedParameters[i] = (IDbDataParameter)((ICloneable)originalParameters[i]).Clone();
            }

            return clonedParameters;
        }
        bool IsParameterCached(string procName)
        {
            string key = CnnString + ":" + procName;
            return paramCache.ContainsKey(key);
        }
        void SetParameterCached(IDbCommand cmd)
        {
            string key = CnnString + ":" + cmd.CommandText;
            List<IDbDataParameter> lst = new List<IDbDataParameter>();
            foreach (IDbDataParameter item in cmd.Parameters)
            {
                lst.Add(item);
            }
            paramCache.Add(key, lst.ToArray());
        }
        void GetCachedParameterSet(IDbCommand cmd)
        {
            string key = CnnString + ":" + cmd.CommandText;
            IDbDataParameter[] param = null;
            if (paramCache.ContainsKey(key) == true)
            {
                param = (IDbDataParameter[])paramCache[key];
            }

            if (param != null)
            {
                IDbDataParameter[] prm = CloneParameters(param);
                foreach (IDbDataParameter item in prm)
                {
                    cmd.Parameters.Add(item);
                }
            }
        }
        #endregion

        #region TransActions
        public IDbTransaction OpenSession()
        {
            IDbConnection cn = GetConnection();
            IDbTransaction trn = cn.BeginTransaction();
            return trn;
        }
        #endregion

        #region ExecuteNonQuery
        public int ExecuteNonQuery(string commandText)
        {
            int sonuc = -1;
            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand command = createdObject.CreateCommand(commandText))
                {
                    command.Connection = cn;
                    command.CommandType = CommandType.Text;
                    sonuc = command.ExecuteNonQuery();
                }
            }
            return sonuc;
        }
        public int ExecuteNonQuery(CommandType commandType, string commandText)
        {
            int sonuc = -1;
            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand command = createdObject.CreateCommand(commandType, commandText))
                {
                    command.Connection = cn;
                    sonuc = command.ExecuteNonQuery();
                }
            }
            return sonuc;
        }
        public int ExecuteNonQuery(string commandText, IDbTransaction trn)
        {
            int sonuc = -1;
            using (IDbCommand command = createdObject.CreateCommand(CommandType.Text, commandText))
            {
                command.Connection = trn.Connection;
                command.Transaction = trn;
                sonuc = command.ExecuteNonQuery();
            }
            return sonuc;
        }
        public int ExecuteNonQuery(CommandType commandType, string commandText, IDbTransaction trn)
        {
            int sonuc = -1;
            using (IDbCommand command = createdObject.CreateCommand(commandType, commandText))
            {
                command.CommandType = commandType;
                command.Connection = trn.Connection;
                command.Transaction = trn;
                sonuc = command.ExecuteNonQuery();
            }
            return sonuc;
        }

        public int ExecuteNonQuery(string ProcName, CommandType commandType, IDbTransaction trn, params object[] value)
        {
            int sonuc = -1;
            using (IDbCommand command = createdObject.CreateCommand(commandType, ProcName))
            {
                command.Connection = trn.Connection;
                command.Transaction = trn;
                if (IsParameterCached(ProcName))
                {
                    GetCachedParameterSet(command);
                }
                else
                {
                    createdObject.DeriveCommand(command);
                    SetParameterCached(command);
                }
                for (int i = 0; i < value.Length; i++)
                {
                    IDbDataParameter prm = (IDbDataParameter)command.Parameters[i + 1];
                    prm.Value = value[i];
                }
                sonuc = command.ExecuteNonQuery();
            }
            return sonuc;
        }
        public int ExecuteNonQuery(CommandType commandType, string commandText, IDbTransaction trn, params IDbDataParameter[] value)
        {
            int sonuc = -1;
            using (IDbCommand command = createdObject.CreateCommand(commandType, commandText))
            {
                command.Connection = trn.Connection;
                command.Transaction = trn;
                foreach (IDbDataParameter item in value)
                {
                    command.Parameters.Add(item);
                }
                sonuc = command.ExecuteNonQuery();
                command.Parameters.Clear();
            }
            return sonuc;
        }

        public int ExecuteNonQuery(string ProcName, CommandType commandType, params object[] value)
        {
            int sonuc = -1;
            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand command = createdObject.CreateCommand(commandType, ProcName))
                {
                    command.Connection = cn;
                    if (IsParameterCached(ProcName))
                    {
                        GetCachedParameterSet(command);
                    }
                    else
                    {
                        createdObject.DeriveCommand(command);
                        SetParameterCached(command);
                    }
                    for (int i = 0; i < value.Length; i++)
                    {
                        IDbDataParameter prm = (IDbDataParameter)command.Parameters[i + 1];
                        prm.Value = value[i];
                    }
                    sonuc = command.ExecuteNonQuery();
                }
            }
            return sonuc;
        }
        public int ExecuteNonQuery(CommandType commandType, string commandText, params IDbDataParameter[] value)
        {
            int sonuc = -1;

            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand command = createdObject.CreateCommand(commandType, commandText))
                {
                    command.Connection = cn;
                    foreach (IDbDataParameter item in value)
                    {
                        command.Parameters.Add(item);
                    }
                    sonuc = command.ExecuteNonQuery();
                    command.Parameters.Clear();
                }
            }
            return sonuc;
        }


        #endregion

        #region Execute Readers
        public IDataReader ExecuteReader(string commandText)
        {
            IDataReader rd = createdObject.CreateReader();
            IDbConnection cn = GetConnection();
            using (IDbCommand cmd = createdObject.CreateCommand(commandText))
            {
                cmd.Connection = cn;
                cmd.CommandType = CommandType.Text;
                rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            return rd;
        }
        public IDataReader ExecuteReader(string commandText, IDbTransaction trn)
        {
            IDataReader rd = createdObject.CreateReader();
            using (IDbCommand cmd = createdObject.CreateCommand())
            {
                cmd.Connection = trn.Connection;
                cmd.Transaction = trn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = commandText;
                rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            return rd;
        }
        public IDataReader ExecuteReader(CommandType commandType, string commandText)
        {
            IDataReader rd = createdObject.CreateReader();
            IDbConnection cn = GetConnection();
            using (IDbCommand cmd = createdObject.CreateCommand())
            {
                cmd.Connection = cn;
                cmd.CommandType = commandType;
                cmd.CommandText = commandText;
                rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            return rd;
        }
        public IDataReader ExecuteReader(CommandType commandType, string commandText, IDbTransaction trn)
        {
            IDataReader rd = createdObject.CreateReader();
            using (IDbCommand cmd = createdObject.CreateCommand())
            {
                cmd.Connection = trn.Connection;
                cmd.Transaction = trn;
                cmd.CommandType = commandType;
                cmd.CommandText = commandText;
                rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            return rd;
        }

        public IDataReader ExecuteReader(string ProcName, CommandType commandType, IDbTransaction trn, params object[] value)
        {
            IDataReader rd = createdObject.CreateReader();
            using (IDbCommand command = createdObject.CreateCommand(commandType, ProcName))
            {
                command.Connection = trn.Connection;
                command.Transaction = trn;
                if (IsParameterCached(ProcName))
                {
                    GetCachedParameterSet(command);
                }
                else
                {
                    createdObject.DeriveCommand(command);
                    SetParameterCached(command);
                }
                for (int i = 0; i < value.Length; i++)
                {
                    IDbDataParameter prm = (IDbDataParameter)command.Parameters[i + 1];
                    prm.Value = value[i];
                }
                rd = command.ExecuteReader(CommandBehavior.CloseConnection);
            }
            return rd;
        }
        public IDataReader ExecuteReader(CommandType commandType, string commandText, IDbTransaction trn, params IDbDataParameter[] value)
        {
            IDataReader rd = createdObject.CreateReader();
            using (IDbCommand command = createdObject.CreateCommand(commandType, commandText))
            {
                command.Connection = trn.Connection;
                command.Transaction = trn;
                foreach (IDbDataParameter item in value)
                {
                    command.Parameters.Add(item);
                }
                rd = command.ExecuteReader(CommandBehavior.CloseConnection);
            }
            return rd;
        }

        public IDataReader ExecuteReader(string ProcName, CommandType commandType, params object[] value)
        {
            IDataReader rd = createdObject.CreateReader();
            IDbConnection cn = GetConnection();
            using (IDbCommand command = createdObject.CreateCommand(commandType, ProcName))
            {
                command.Connection = cn;
                if (IsParameterCached(ProcName))
                {
                    GetCachedParameterSet(command);
                }
                else
                {
                    createdObject.DeriveCommand(command);
                    SetParameterCached(command);
                }
                for (int i = 0; i < value.Length; i++)
                {
                    IDbDataParameter prm = (IDbDataParameter)command.Parameters[i + 1];
                    prm.Value = value[i];
                }
                rd = command.ExecuteReader(CommandBehavior.CloseConnection);
            }
            return rd;

        }
        public IDataReader ExecuteReader(CommandType commandType, string commandText, params IDbDataParameter[] value)
        {
            IDataReader rd = createdObject.CreateReader();
            IDbConnection cn = GetConnection();
            using (IDbCommand command = createdObject.CreateCommand(commandType, commandText))
            {
                command.Connection = cn;
                foreach (IDbDataParameter item in value)
                {
                    command.Parameters.Add(item);
                }
                rd = command.ExecuteReader(CommandBehavior.CloseConnection);
            }
            return rd;
        }


        #endregion

        #region Execute Scaler
        public object ExecuteScaler(string commandText)
        {
            object sc = null;
            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand cmd = createdObject.CreateCommand(commandText))
                {
                    cmd.Connection = cn;
                    cmd.CommandType = CommandType.Text;
                    sc = cmd.ExecuteScalar();
                }
            }
            return sc;
        }
        public object ExecuteScaler(CommandType commandType, string commandText)
        {
            object sc = null;
            using (IDbConnection cn = GetConnection())
            {
                IDbCommand cmd = createdObject.CreateCommand();
                cmd.Connection = cn;
                cmd.CommandType = commandType;
                cmd.CommandText = commandText;
                sc = cmd.ExecuteScalar();
            }
            return sc;
        }
        public object ExecuteScaler(string commandText, IDbTransaction trn)
        {
            object sc = null;
            using (IDbCommand cmd = createdObject.CreateCommand())
            {
                cmd.Connection = trn.Connection;
                cmd.Transaction = trn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = commandText;
                sc = cmd.ExecuteScalar();
            }
            return sc;
        }
        public object ExecuteScaler(CommandType commandType, string commandText, IDbTransaction trn)
        {
            object sc = null;
            using (IDbCommand cmd = createdObject.CreateCommand())
            {
                cmd.Connection = trn.Connection;
                cmd.Transaction = trn;
                cmd.CommandType = commandType;
                cmd.CommandText = commandText;
                sc = cmd.ExecuteScalar();
            }
            return sc;
        }


        public object ExecuteScaler(string ProcName, CommandType commandType, params object[] value)
        {
            object sc = null;
            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand command = createdObject.CreateCommand(commandType, ProcName))
                {
                    command.Connection = cn;
                    if (IsParameterCached(ProcName))
                    {
                        GetCachedParameterSet(command);
                    }
                    else
                    {
                        createdObject.DeriveCommand(command);
                        SetParameterCached(command);
                    }
                    for (int i = 0; i < value.Length; i++)
                    {
                        IDbDataParameter prm = (IDbDataParameter)command.Parameters[i + 1];
                        prm.Value = value[i];
                    }
                    sc = command.ExecuteScalar();
                    command.Parameters.Clear();
                }
            }
            return sc;
        }
        public object ExecuteScaler(CommandType commandType, string commandText, params IDbDataParameter[] value)
        {
            object sc = null;
            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand command = createdObject.CreateCommand(commandType, commandText))
                {
                    command.Connection = cn;
                    foreach (IDbDataParameter item in value)
                    {
                        command.Parameters.Add(item);
                    }
                    sc = command.ExecuteScalar();
                    command.Parameters.Clear();
                }
            }
            return sc;
        }

        public object ExecuteScaler(string ProcName, CommandType commandType, IDbTransaction trn, params object[] value)
        {
            object sc = null;
            using (IDbCommand command = createdObject.CreateCommand(commandType, ProcName))
            {
                command.Connection = trn.Connection;
                command.Transaction = trn;
                if (IsParameterCached(ProcName))
                {
                    GetCachedParameterSet(command);
                }
                else
                {
                    createdObject.DeriveCommand(command);
                    SetParameterCached(command);
                }
                for (int i = 0; i < value.Length; i++)
                {
                    IDbDataParameter prm = (IDbDataParameter)command.Parameters[i + 1];
                    prm.Value = value[i];
                }
                sc = command.ExecuteScalar();
            }
            return sc;
        }
        public object ExecuteScaler(CommandType commandType, string commandText, IDbTransaction trn, params IDbDataParameter[] value)
        {
            object sc = null;
            using (IDbCommand command = createdObject.CreateCommand(commandType, commandText))
            {
                command.Connection = trn.Connection;
                command.Transaction = trn;
                foreach (IDbDataParameter item in value)
                {
                    command.Parameters.Add(item);
                }
                sc = command.ExecuteScalar();
            }
            return sc;
        }
        #endregion

        #region Execute DataTable
        public DataTable ExecuteDataTable(string CommandText)
        {
            DataSet ds = new DataSet();
            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand cmd = createdObject.CreateCommand())
                {
                    cmd.Connection = cn;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = CommandText;
                    IDbDataAdapter da = createdObject.CreateAdapter(cmd);
                    da.Fill(ds);
                }
            }
            return ds.Tables[0];
        }
        public DataTable ExecuteDataTable(CommandType cmdtype, string CommandText)
        {
            DataSet dt = new DataSet();
            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand cmd = createdObject.CreateCommand(cmdtype, CommandText))
                {
                    cmd.Connection = cn;
                    IDbDataAdapter da = createdObject.CreateAdapter(cmd);
                    da.Fill(dt);
                }
            }
            return dt.Tables[0];
        }
        public DataTable ExecuteDataTable(string commandText, IDbTransaction trn)
        {
            DataSet ds = new DataSet();
            using (IDbCommand cmd = createdObject.CreateCommand())
            {
                cmd.Connection = trn.Connection;
                cmd.Transaction = trn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = commandText;
                IDbDataAdapter da = createdObject.CreateAdapter(cmd);
                da.Fill(ds);
            }
            return ds.Tables[0];
        }
        public DataTable ExecuteDataTable(CommandType commandType, string commandText, IDbTransaction trn)
        {
            DataSet ds = new DataSet();
            using (IDbCommand cmd = createdObject.CreateCommand())
            {
                cmd.Connection = trn.Connection;
                cmd.Transaction = trn;
                cmd.CommandType = commandType;
                cmd.CommandText = commandText;
                IDbDataAdapter da = createdObject.CreateAdapter(cmd);
                da.Fill(ds);
            }
            return ds.Tables[0];
        }


        public DataTable ExecuteDataTable(string ProcName, CommandType commandType, params object[] value)
        {
            DataSet ds = new DataSet();
            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand command = createdObject.CreateCommand(commandType, ProcName))
                {
                    command.Connection = cn;
                    if (IsParameterCached(ProcName))
                    {
                        GetCachedParameterSet(command);
                    }
                    else
                    {
                        createdObject.DeriveCommand(command);
                        SetParameterCached(command);
                    }
                    for (int i = 0; i < value.Length; i++)
                    {
                        IDbDataParameter prm = (IDbDataParameter)command.Parameters[i + 1];
                        prm.Value = value[i];
                    }
                    IDbDataAdapter da = createdObject.CreateAdapter(command);
                    da.Fill(ds);
                }
            }
            return ds.Tables[0];
        }
        public DataTable ExecuteDataTable(CommandType commandType, string commandText, params IDbDataParameter[] value)
        {
            DataSet ds = new DataSet();
            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand command = createdObject.CreateCommand(commandType, commandText))
                {
                    command.Connection = cn;
                    foreach (IDbDataParameter item in value)
                    {
                        command.Parameters.Add(item);
                    }
                    IDataAdapter da = createdObject.CreateAdapter(command);
                    da.Fill(ds);
                    command.Parameters.Clear();
                }
            }
            return ds.Tables[0];
        }

        public DataTable ExecuteDataTable(string ProcName, CommandType commandType, IDbTransaction trn, params object[] value)
        {
            DataSet ds = new DataSet();
            using (IDbCommand command = createdObject.CreateCommand(commandType, ProcName))
            {
                command.Connection = trn.Connection;
                command.Transaction = trn;
                if (IsParameterCached(ProcName))
                {
                    GetCachedParameterSet(command);
                }
                else
                {
                    createdObject.DeriveCommand(command);
                    SetParameterCached(command);
                }
                for (int i = 0; i < value.Length; i++)
                {
                    IDbDataParameter prm = (IDbDataParameter)command.Parameters[i + 1];
                    prm.Value = value[i];
                }
                IDbDataAdapter da = createdObject.CreateAdapter(command);
                da.Fill(ds);
            }
            return ds.Tables[0];

        }
        public DataTable ExecuteDataTable(CommandType commandType, string commandText, IDbTransaction trn, params IDbDataParameter[] value)
        {
            DataSet ds = new DataSet();
            using (IDbCommand command = createdObject.CreateCommand(commandType, commandText))
            {
                command.Connection = trn.Connection;
                command.Transaction = trn;
                foreach (IDbDataParameter item in value)
                {
                    command.Parameters.Add(item);
                }
                IDataAdapter da = createdObject.CreateAdapter(command);
                da.Fill(ds);
            }
            return ds.Tables[0];
        }
        #endregion

        #region Execute DataSet
        public DataSet ExecuteDataSet(string CommandText)
        {
            DataSet ds = new DataSet();
            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand cmd = createdObject.CreateCommand())
                {
                    cmd.Connection = cn;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = CommandText;
                    IDbDataAdapter da = createdObject.CreateAdapter(cmd);
                    da.Fill(ds);

                }
            }
            return ds;
        }
        public DataSet ExecuteDataSet(CommandType cmdtype, string CommandText)
        {
            DataSet dt = new DataSet();
            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand cmd = createdObject.CreateCommand(cmdtype, CommandText))
                {
                    IDbDataAdapter da = createdObject.CreateAdapter(cmd);

                    da.Fill(dt);

                }
            }
            return dt;
        }
        public DataSet ExecuteDataSet(string commandText, IDbTransaction trn)
        {
            DataSet ds = new DataSet();
            using (IDbCommand cmd = createdObject.CreateCommand())
            {
                cmd.Connection = trn.Connection;
                cmd.Transaction = trn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = commandText;
                IDbDataAdapter da = createdObject.CreateAdapter(cmd);
                da.Fill(ds);
            }
            return ds;
        }
        public DataSet ExecuteDataSet(CommandType commandType, string commandText, IDbTransaction trn)
        {
            DataSet ds = new DataSet();
            using (IDbCommand cmd = createdObject.CreateCommand())
            {
                cmd.Connection = trn.Connection;
                cmd.Transaction = trn;
                cmd.CommandType = commandType;
                cmd.CommandText = commandText;
                IDbDataAdapter da = createdObject.CreateAdapter(cmd);
                da.Fill(ds);
            }
            return ds;
        }

        public DataSet ExecuteDataSet(string ProcName, CommandType commandType, params object[] value)
        {
            DataSet ds = new DataSet();
            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand command = createdObject.CreateCommand(commandType, ProcName))
                {
                    command.Connection = cn;
                    if (IsParameterCached(ProcName))
                    {
                        GetCachedParameterSet(command);
                    }
                    else
                    {
                        createdObject.DeriveCommand(command);
                        SetParameterCached(command);
                    }
                    for (int i = 0; i < value.Length; i++)
                    {
                        IDbDataParameter prm = (IDbDataParameter)command.Parameters[i + 1];
                        prm.Value = value[i];
                    }
                    IDbDataAdapter da = createdObject.CreateAdapter(command);
                    da.Fill(ds);
                }
            }
            return ds;
        }
        public DataSet ExecuteDataSet(CommandType commandType, string commandText, params IDbDataParameter[] value)
        {
            DataSet ds = new DataSet();
            using (IDbConnection cn = GetConnection())
            {
                using (IDbCommand command = createdObject.CreateCommand(commandType, commandText))
                {
                    command.Connection = cn;
                    foreach (IDbDataParameter item in value)
                    {
                        command.Parameters.Add(item);
                    }
                    IDataAdapter da = createdObject.CreateAdapter(command);
                    da.Fill(ds);
                }
            }
            return ds;
        }

        public DataSet ExecuteDataSet(string ProcName, CommandType commandType, IDbTransaction trn, params object[] value)
        {
            DataSet ds = new DataSet();
            using (IDbCommand command = createdObject.CreateCommand(commandType, ProcName))
            {
                command.Connection = trn.Connection;
                command.Transaction = trn;
                if (IsParameterCached(ProcName))
                {
                    GetCachedParameterSet(command);
                }
                else
                {
                    createdObject.DeriveCommand(command);
                    SetParameterCached(command);
                }
                for (int i = 0; i < value.Length; i++)
                {
                    IDbDataParameter prm = (IDbDataParameter)command.Parameters[i + 1];
                    prm.Value = value[i];
                }
                IDbDataAdapter da = createdObject.CreateAdapter(command);
                da.Fill(ds);
            }

            return ds;
        }
        public DataSet ExecuteDataSet(CommandType commandType, string commandText, IDbTransaction trn, params IDbDataParameter[] value)
        {
            DataSet ds = new DataSet();
            using (IDbCommand command = createdObject.CreateCommand(commandType, commandText))
            {
                command.Connection = trn.Connection;
                command.Transaction = trn;
                foreach (IDbDataParameter item in value)
                {
                    command.Parameters.Add(item);
                }
                IDataAdapter da = createdObject.CreateAdapter(command);
                da.Fill(ds);
            }
            return ds;
        }
        #endregion

    }
}
